<?php
namespace app\api\controller;

use library\Controller;
use think\Db;
use think\facade\Cache;

/**
 * 充值
 */
class Recharge extends Controller{
    /**
     * Describe:充值
     * DateTime: 
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function recharge(){
        $params = $this->request->param();
        $this->checkToken();
        $uid = $this->userInfo['id'];
        $money = $params['money']??0;
        $info = Db::name('LcInfo')->find(1);
        
        //判断认证状态
        if(getUserNeedAuth($uid)) $this->error('auth.authFirst',"",405);
        
        //判断参数
        if($money<1) $this->error('utils.parameterError',"",218);
        if($money<$info['recharge_min']){
            $this->error("最小充值{$info['recharge_min']}","",218);
        }

        //不能让一个用户占用太多充值值
        $c = Db::name('LcUserRechargeRecord')->where("status = 0 and uid = {$uid}")->count();
        if($c>0){
            $this->error('您已有在支付中的订单！',"",218);
        }

        //获取一个金额
        $money = $this->getRechargeMoney($money);
         
        $orderNo = 'IN' . date('YmdHis') . rand(1000, 9999) . rand(100, 999);
        $time = date('Y-m-d H:i:s');
        
        //添加充值记录
        $insert = array(
            "uid" =>$uid,
            "orderNo" =>$orderNo,
            "money" =>$money,
            "money2" =>$money,
            "act_time" =>$time,
            "time" =>$time
        );
        $rrid = Db::name('LcUserRechargeRecord')->insertGetId($insert);
        if(!empty($rrid)){
            $insert['id'] = $rrid;
            $this->success("success",$insert);
        }
        $this->error("error");
    }
    /**
     * 获取充值金额
     */
    private function getRechargeMoney($money){
        $money = (int)$money;
        $number = Cache::inc("recharge_money:{$money}");
        $money = $money+($number%1000)*0.001;
        $c = Db::name('LcUserRechargeRecord')->where("status = 0 and money = {$money}")->count();
        if($c>0){
            $this->error('系统繁忙，请稍候再试！',"",218);
        }
        return $money;
    }

    /**
     * Describe:充值记录
     * DateTime: 
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function record(){
        $params = $this->request->param();
        $this->checkToken();
        $uid = $this->userInfo['id'];
        
        $page = $params["page"];
        $listRows = $params["listRows"];
        
        $list = Db::name('LcUserRechargeRecord urr')->where("urr.uid = $uid  AND urr.money > 0")->order("urr.act_time desc")->page($page,$listRows)->select();
        $length = Db::name('LcUserRechargeRecord urr')->where("urr.uid = $uid AND urr.money > 0")->order("urr.act_time desc")->count();
        
        $data = array(
            'list' => $list,
            'length' => $length
        );
        $this->success("success", $data);
    }

    /**
     * Describe:获取充值详情
     * DateTime: 
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function recordDetail(){
        $params = $this->request->param();
        $this->checkToken();
        $uid = $this->userInfo['id'];
        $id = $params['id']??0;
        
        $data = Db::name('LcUserRechargeRecord')->where("uid = $uid AND id = $id")->find();

        $this->success("success", $data);
    }
    /**
     * 取消订单
     */
    public function cancel(){
        $params = $this->request->param();
        $this->checkToken();
        $uid = $this->userInfo['id'];
        $id = $params['id']??0;

        $timeOf = date('Y-m-d H:00:00');
        $c = Db::name('LcUserRechargeRecord')->where("uid = $uid AND status = 3 AND time > '$timeOf'")->count();
        if($c>5){
            $this->error('请不要频繁地取消订单！',"",218);
        }
        Db::name('LcUserRechargeRecord')->where("uid = $uid AND id = $id AND status = 0")->update(['status'=>3]);
        $this->success("success");
    }
}
?>